Skip to content

Comments

[release/10.0] Fix InvalidCastException in OriginalValues.ToObject() with nested nullable complex properties#37703

Open
Copilot wants to merge 1 commit intorelease/10.0from
copilot/fix-invalid-cast-exception
Open

[release/10.0] Fix InvalidCastException in OriginalValues.ToObject() with nested nullable complex properties#37703
Copilot wants to merge 1 commit intorelease/10.0from
copilot/fix-invalid-cast-exception

Conversation

Copy link
Contributor

Copilot AI commented Feb 13, 2026

Fixes #37516
Port of #37762

Description

When calling context.Entry(...).OriginalValues.ToObject(), an InvalidCastException is thrown when entities have nested nullable complex properties and any of those nested properties are null.

Customer impact

This prevents customers from using OriginalValues.ToObject() with nested nullable complex properties.

Workaround: Customers can set the AppContext switch Microsoft.EntityFrameworkCore.Issue37516 to true to revert to the old behavior, but this disables the nullable complex property feature added in 10.0.2.

How found

Customer reported on 10.0.2.

Regression

Yes, this is a regression from 10.0.1, introduced in #37302

Testing

Tests added.

Risk

Low. Quirk mode added.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix InvalidCastException with complex properties in 10.0.2 Fix InvalidCastException in OriginalValues.ToObject() with nested nullable complex properties Feb 13, 2026
Copilot AI requested a review from AndriySvyryd February 13, 2026 22:31
@AndriySvyryd
Copy link
Member

@copilot Update progress by rewriting the PR summary using this template:

Fixes #{number}

**Description**
{Issue description}

**Customer impact**
{How does the reported issue affects customer's app? Are there workarounds?}

**How found**
{Was it customer reported or found during verification? How many customers are affected?}

**Regression**
{Is it a regression from a released version? Which one?}

**Testing**
{How the changes were tested}

**Risk**
{Low/Medium/High, argument why}

Copy link
Contributor Author

Copilot AI commented Feb 13, 2026

@copilot Update progress by rewriting the PR summary using this template:

...

Updated PR description with the requested template format. The issue number placeholder (#XXXXX) should be replaced with the actual issue number when available.

Copilot AI requested a review from AndriySvyryd February 14, 2026 06:01
@AndriySvyryd AndriySvyryd changed the title Fix InvalidCastException in OriginalValues.ToObject() with nested nullable complex properties [release/10.0] Fix InvalidCastException in OriginalValues.ToObject() with nested nullable complex properties Feb 18, 2026
…d nullable complex properties

Fixes #37516

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
@AndriySvyryd AndriySvyryd force-pushed the copilot/fix-invalid-cast-exception branch from 9167798 to 60956cf Compare February 20, 2026 21:27
@AndriySvyryd AndriySvyryd marked this pull request as ready for review February 20, 2026 21:28
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner February 20, 2026 21:28
Copilot AI review requested due to automatic review settings February 20, 2026 21:28
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an InvalidCastException that occurs when calling OriginalValues.ToObject() or CurrentValues.ToObject() on entities with nested nullable complex properties where intermediate or leaf properties are null. This is a regression introduced in PR #37302 (v10.0.2).

Changes:

  • Fixed ArrayPropertyValues.ToObject() to correctly navigate nested complex property hierarchies when setting null values
  • Added NavigateToDeclaringType helper to correctly read values from nested complex properties
  • Added comprehensive test coverage for various nested complex property scenarios
  • Implemented quirk mode switch Microsoft.EntityFrameworkCore.Issue37516 for backward compatibility

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/EFCore/ChangeTracking/Internal/ArrayPropertyValues.cs Adds SetNestedComplexPropertyValue to recursively navigate complex property chains when setting values, and NavigateToDeclaringType to navigate to declaring types when reading values. Includes quirk mode switch for backward compatibility.
test/EFCore.Tests/ChangeTracking/Internal/PropertyValuesTest.cs Refactors existing tests to use StateManager directly instead of DbContext, and adds comprehensive test coverage for nested nullable complex properties in various configurations (single properties, collections, and nested combinations).
Comments suppressed due to low confidence (1)

test/EFCore.Tests/ChangeTracking/Internal/PropertyValuesTest.cs:192

  • The ClassData attribute should use DataGenerator<bool?> instead of DataGenerator<bool> to match the parameter type bool? useOriginalValues and test all three cases (null, false, true) like the other test methods. Currently, this test only covers false and true, missing the Clone() code path (null case).
    [ClassData(typeof(DataGenerator<bool>))]

return root;
}

if (declaringType is not IComplexType ct)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: merge with the above condition

}

var child = currentProperty.GetGetter().GetClrValue(instance);
if (child == null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Especially because this is an arbitrary object where the equality operator might be overridden.

Suggested change
if (child == null)
if (child is null)

@roji roji assigned AndriySvyryd and unassigned roji Feb 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants